using System;

using System.IO.IsolatedStorage;
using System.Collections.Generic;

using Zsa.Emulation.Util;
using Zsa.Emulation.Render;
using System.IO;


namespace Zsa.Emulation.Config
{

    /// <summary>
    /// Used to read and write configuration stuff.
    /// </summary>
    public static class NesConfigurationManager
    {

        #region Fields
        #region Render Default
        /// <summary>
        /// The default graphics
        /// </summary>
        private static readonly RuntimeTypeHandle _DefaultGraphicsType;
        /// <summary>
        /// The default sound.
        /// </summary>
        private static readonly RuntimeTypeHandle _DefaultAudioType;

        /// <summary>
        /// The default input.
        /// </summary>
        private static readonly RuntimeTypeHandle _DefaultInputType;
        #endregion Render Defaults

        /// <summary>
        /// The configuration store.
        /// </summary>
        private static GenericWeakReference<NesConfigurationStore> _ConfigurationStore;

        /// <summary>
        /// The default profile.
        /// </summary>
        private static NesConfigurationProfile _GlobalDefaultProfile;
        #endregion Fields


        #region Constructor
        /// <summary>
        /// Initializes the <see cref="T:NesConfigurationManager"/> class.
        /// </summary>
        static NesConfigurationManager()
        {
            _DefaultInputType = typeof(Render.Input.DxInput.DxInputCapabilities).TypeHandle;
            _DefaultAudioType = typeof(Render.Audio.DxSound.DxSoundCapabilities).TypeHandle;
            _DefaultGraphicsType = typeof(Render.Video.Direct3d.Dx3dRenderCapabilities).TypeHandle;
        }

        #endregion Constructor

        #region Properties

        /// <summary>
        /// Gets the default.
        /// </summary>
        /// <value>The default.</value>
        public static NesConfigurationProfile Default
        {
            get
            {
                if (_GlobalDefaultProfile == null)
                {
                    _GlobalDefaultProfile = new NesConfigurationProfile(
                        "Default",
                        NesMachineSettings.Default,
                        Render.RenderManager.DefaultRender);
                }
                return _GlobalDefaultProfile;
            }
        }

        /// <summary>
        /// Gets the configuration items.
        /// </summary>
        /// <value>The configuration items.</value>
        public static NesConfigurationStore Profiles
        {
            get
            {
                return new NesConfigurationStore();
                NesConfigurationStore _ProfileTemp = null;
                if (!_ConfigurationStore.TryGetReference(out _ProfileTemp))
                {
                    _ProfileTemp = LoadUserConfiguration();
                }
                return _ProfileTemp;
            }
        }
        
        #endregion Properties

        public static NesConfigurationStore LoadUserConfiguration()
        {
            // Get the user storage.
            IsolatedStorageFile _Storage = IsolatedStorageFile.GetUserStoreForApplication();
                //.GetStore(IsolatedStorageScope.User,
                //typeof(NesConfigurationStore));

            IsolatedStorageFileStream _Stream = null;

            try
            {
                _Stream =
                    new IsolatedStorageFileStream(
                        NesConfigurationStore.DefaultFileName,
                        FileMode.Open,
                        _Storage
                        );
            }
            catch (FileNotFoundException)
            {
                _Stream = new IsolatedStorageFileStream(
                    NesConfigurationStore.DefaultFileName,
                    FileMode.CreateNew,
                    _Storage
                );
            }
            catch ( Exception excep ){
                throw excep;
            }
            return null;
            
        }
        public static NesConfigurationStore CreateNewConfiguration()
        {
            return null;
        }

        private static void Init()
        {
            // Get the application storage
//            IsolatedStorageFile _Storeage = IsolatedStorageFile.GetMachineStoreForApplication();
            
            
        }


        internal static void SaveProfile(NesConfigurationProfile configProfile, bool saveGlobal)
        {

        }



        //private static RenderDevice CreateDefault(RenderDeviceType deviceType)
        //{
            
        //    RenderDevice _Device;
        //    Type _Type = null;

        //    // Get the default type based on the device type.
        //    switch (deviceType)
        //    {
        //        case RenderDeviceType.Audio:
        //            _Type = Type.GetTypeFromHandle(_DefaultAudioType);
        //            break;
        //        case RenderDeviceType.Input:
        //            _Type = Type.GetTypeFromHandle(_DefaultInputType);
        //            break;
        //        case RenderDeviceType.Video:
        //            _Type = Type.GetTypeFromHandle(_DefaultGraphicsType);
        //            break;
        //        default:
        //            throw new NotSupportedException();
        //    }

        //    // Create the device.
        //    try
        //    {
        //        _Device = Activator.CreateInstance(_Type, new object[0]) as RenderDevice;
        //    }
        //    catch (Exception excep)
        //    {
        //        throw excep;
        //    }

        //    return _Device;

        //}



        
    }
}
